<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import RouteDetail from '../components/RouteDetail.vue'
import { useCartStore } from '../store/cart' // 导入购物车 store
import { RoutesApi } from '../api/routes'
const cartStore = useCartStore() // 使用购物车 store

// 线路分类
const categories = ref([
  { id: 1, name: '一日游' },
  { id: 2, name: '周末游' },
  { id: 3, name: '主题游' },
  { id: 4, name: '定制游' },
  { id: 5, name: '世遗文化游' },
  { id: 6, name: '滨海风光游' },
  { id: 7, name: '亲子欢乐游' }
])

// 线路列表
const routes = ref([])

// 当前选中的分类
const currentCategory = ref(categories.value[0].id)

// 详情弹窗控制
const showRouteDetail = ref(false)
const selectedRouteId = ref(0)

// 打开线路详情
const openRouteDetail = (routeId) => {
  selectedRouteId.value = routeId;
  showRouteDetail.value = true;
};

// 关闭线路详情
const closeRouteDetail = () => {
  showRouteDetail.value = false;
};

// 根据分类筛选线路
const filteredRoutes = computed(() => {
  return currentCategory.value === 0
    ? routes.value
    : routes.value.filter(route => route.category === currentCategory.value)
})

onMounted(() => {
  handleGetRoutes()
  // 触发标题动画
  const titleChars = document.querySelectorAll('.main-title span');
  titleChars.forEach((char, index) => {
    (char as HTMLElement).style.animationDelay = `${index * 0.1}s`;
  });
  
  // 触发卡片入场动画
  const cards = document.querySelectorAll('.route-card');
  cards.forEach((card, index) => {
    (card as HTMLElement).style.animationDelay = `${index * 0.1 + 0.5}s`;
  });
});


// 登录方法
const handleGetRoutes = async () => {
  
      const response = await RoutesApi.getRoutes();
  
          routes.value=response.data.data.list
      console.log(response);
     
    
  
};


// 加入购物车
const addToCart = (route) => {
  cartStore.addItem(route)
  alert(`${route.title} 已加入购物车！`)
}
</script>

<template>
  <div class="routes-container">
    <!-- 动态标题头 -->
    <div class="routes-header">
      <h1 class="main-title">
        <span>线</span><span>路</span><span>推</span><span>荐</span>
      </h1>
      <p class="subtitle">探索泉州，发现美好旅程</p>
    </div>
    
    <!-- 分类选择 -->
    <div class="categories">
      <button
        v-for="category in categories"
        :key="category.id"
        :class="['category-btn', { active: currentCategory === category.id }]"
        @click="currentCategory = category.id"
      >
        {{ category.name }}
      </button>
    </div>

    <!-- 线路列表 -->
    <div class="routes-list">
      <div v-for="(route, index) in routes" :key="route.id" class="route-card" :style="{ '--card-index': index }">
        <div class="route-image">
          <img :src="route.coverImage" :alt="route.title">
          <span class="duration">{{ route.duration }}</span>
          <div class="route-overlay">
            <button class="view-details-btn" @click="openRouteDetail(route.id)">查看详情</button>
            <button class="add-to-cart-btn" @click="addToCart(route)">加入购物车</button>
          </div>
        </div>
        <div class="route-info">
          <h3 class="route-title">{{ route.routeName }}</h3>
           <h3 class="route-title">天数：{{ route.day }}</h3>
          <p class="route-price">¥{{ route.price }}</p>
          <div class="route-meta">
            <span class="route-rating">评分: 4.8</span>
            <span class="route-sold">已售: 20</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 线路详情弹窗 -->
    <RouteDetail :routeId="selectedRouteId" :showDetail="showRouteDetail" @close="closeRouteDetail" />
  </div>
</template>

<style scoped>
/* 页面整体布局 */
.routes-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
  font-family: 'Arial', sans-serif;
  color: #333;
}

/* 标题部分 */
.routes-header {
  text-align: center;
  margin-bottom: 40px;
  overflow: hidden; /* 确保动画不会溢出 */
}

.main-title {
  font-size: 3.5em;
  color: #2c3e50;
  margin-bottom: 10px;
  font-weight: bold;
  display: inline-block; /* 允许 span 元素在同一行 */
}

.main-title span {
  display: inline-block;
  opacity: 0;
  transform: translateY(20px);
  animation: slideInUp 0.8s forwards ease-out;
}

@keyframes slideInUp {
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.subtitle {
  font-size: 1.2em;
  color: #7f8c8d;
}

/* 分类按钮 */
.categories {
  text-align: center;
  margin-bottom: 30px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
}

.category-btn {
  background-color: #ecf0f1;
  color: #34495e;
  border: none;
  padding: 10px 20px;
  border-radius: 25px;
  cursor: pointer;
  font-size: 1em;
  transition: all 0.3s ease;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

.category-btn:hover {
  background-color: #bdc3c7;
  transform: translateY(-2px);
}

.category-btn.active {
  background-color: #3498db;
  color: white;
  box-shadow: 0 4px 10px rgba(52, 152, 219, 0.4);
  transform: translateY(-2px);
}

/* 线路列表 */
.routes-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 30px;
}

.route-card {
  background-color: white;
  border-radius: 15px;
  overflow: hidden;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  animation: fadeInScale 0.6s forwards ease-out;
  opacity: 0;
  transform: translateY(20px) scale(0.95);
  animation-delay: calc(var(--card-index) * 0.1s + 0.5s); /* 延迟动画 */
}

@keyframes fadeInScale {
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.route-card:hover {
  transform: translateY(-5px) scale(1.02);
  box-shadow: 0 12px 25px rgba(0, 0, 0, 0.15);
}

.route-image {
  position: relative;
  width: 100%;
  height: 200px;
  overflow: hidden;
}

.route-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.5s ease;
}

.route-card:hover .route-image img {
  transform: scale(1.05);
}

.duration {
  position: absolute;
  top: 15px;
  left: 15px;
  background-color: rgba(52, 152, 219, 0.9);
  color: white;
  padding: 5px 12px;
  border-radius: 20px;
  font-size: 0.9em;
  font-weight: bold;
  z-index: 1;
}

.route-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column; /* 垂直排列按钮 */
  justify-content: center;
  align-items: center;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.route-card:hover .route-overlay {
  opacity: 1;
}

.view-details-btn,
.add-to-cart-btn { /* 统一按钮样式 */
  background-color: #3498db;
  color: white;
  border: none;
  padding: 12px 25px;
  border-radius: 30px;
  cursor: pointer;
  font-size: 1.1em;
  font-weight: bold;
  transition: background-color 0.3s ease, transform 0.3s ease;
  margin: 5px 0; /* 按钮间距 */
}

.view-details-btn:hover,
.add-to-cart-btn:hover {
  background-color: #2980b9;
  transform: translateY(-2px);
}

.route-info {
  padding: 20px;
}

.route-title {
  font-size: 1.5em;
  margin-bottom: 10px;
  color: #2c3e50;
}

.route-price {
  font-size: 1.8em;
  color: #e74c3c;
  font-weight: bold;
  margin-bottom: 10px;
}

.route-meta {
  display: flex;
  justify-content: space-between;
  font-size: 0.9em;
  color: #7f8c8d;
}

.route-rating,
.route-sold {
  background-color: #f2f2f2;
  padding: 5px 10px;
  border-radius: 15px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .main-title {
    font-size: 2.5em;
  }

  .subtitle {
    font-size: 1em;
  }

  .routes-list {
    grid-template-columns: 1fr;
  }

  .category-btn {
    padding: 8px 15px;
    font-size: 0.9em;
  }

  .view-details-btn,
  .add-to-cart-btn {
    padding: 10px 20px;
    font-size: 1em;
  }
}

@media (max-width: 480px) {
  .routes-container {
    padding: 15px;
  }

  .main-title {
    font-size: 2em;
  }

  .categories {
    gap: 8px;
  }
}
</style>